<?php
namespace Admin\Controller;
use Tools\AdminController,    Model\RoleModel,
    Model\AuthModel;

class RoleController extends AdminController {
    
    function showlist(){
        $romd = new RoleModel();
        
        $list  = $romd -> where('role_group=0') -> select();
        $qlist = $romd -> where('role_group=1') -> select();
        
        $this -> assign('list',$list);
        $this -> assign('qlist',$qlist);
        $this -> display();
    }

    
    function add(){
        $romd = new RoleModel();
        
        if(!empty($_POST)){
            $shu = $romd -> create();
            if($shu){
                if($romd -> add($shu)){
                    $this ->redirect('showlist');
                }
            }else {
                $error = $romd -> getError();
                $this  -> alert($error); 
            }
        }
        
        $this -> display();
    }
    
    
    function delete($id){
        $romd = new RoleModel();
        
        if($romd -> delete($id)){
            $this ->redirect('showlist');
        }else{
            $this ->redirect('showlist','',3,'删除失败');
        }
        
    }
    
    
    function xiugai($id){
        $romd = new RoleModel();
            
        //查找修改角色
        $role = $romd -> find($id);
        //修改
        if(!empty($_POST)){
            $shu = $romd -> create();
            if($shu){
                if($romd -> save($shu)){
                    $this ->redirect('showlist');
                }else{
                    $this ->redirect('showlist');
                }
            }else {
                $error = $romd -> getError();
                $this  -> alert($error); 
            }
        }
               
        $this -> assign('role',$role);
        $this -> display();
    }
    
    
    function distribute($role_id){
        //为角色分配权限
        $aumd = new AuthModel();
        $romd = new RoleModel();
        
        //要分配权限的角色
        $role  = $romd -> find($role_id);
        $auths = $role['role_auth_ids'];
        $auths = explode(',', $auths);
        
        //所有权限
        $flist = $aumd -> where('auth_level=0') -> select();
        $zlist = $aumd -> where('auth_level=1') -> select();
        
        //收集
        if(!empty($_POST)){
            $bool = $romd -> saveAuth($_POST['role_id'],$_POST['auth_id']);
            if($bool) {
                $this ->redirect('Role/showlist');
            }else {
                $this ->redirect('Role/showlist');
            }
        }

        $this -> assign('flist',$flist);
        $this -> assign('zlist',$zlist);
        $this -> assign('role',$role);
        $this -> assign('auths',$auths);
        $this -> display();
    }

    
}